﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BTalk.Crypto.Algorithms
{
    public class Vigenere : ICryptoStrategy
    {
        public byte[] Key { get; set; }

        public Vigenere(byte[] key)
        {
            this.Key = key;
        }

        public byte[] Encode(byte[] message)
        {
            return ExpandKey(Key, message).Zip(message, (k, v) => (byte)(k + v)).ToArray();
        }

        public byte[] Decode(byte[] message)
        {
            return ExpandKey(Key, message).Zip(message, (k, v) => (byte)(v - k)).ToArray();
        }

        private byte[] ExpandKey(byte[] originalKey, byte[] message)
        {
            byte[] key = new byte[message.Length];

            for (int i = 0; i < key.Length; ++i)
            {
                key[i] = originalKey[i % originalKey.Length];
            }

            return key;
        }

        public dynamic Configuration
        {
            get;
            set;
        }
    }
}
